
package com.insigma.business.qrcode.soldier._bff.impl;


import cn.hutool.core.bean.BeanUtil;

import com.insigma.business.qrcode.common._consumer.CommonAdminAreaConsumer;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.SessionUtils;
import com.insigma.business.qrcode.common.vo.BaseDeleteRequest;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.soldier.dto.*;
import com.insigma.business.qrcode.soldier.dto.SoldierMessageInfoUpdateRequest;
import com.insigma.business.qrcode.soldier.dto.gov.BaseProviderImpl;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.soldier.request.*;
import com.insigma.business.qrcode.soldier._bff.SoldierMessageInfoBFFProvider;
import com.insigma.business.qrcode.soldier._consumer.SoldierMessageInfoConsumer;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;


@Slf4j
@Service
@Component("soldierMessageInfoBFFProvider")
public class SoldierMessageInfoBFFProviderImpl extends BaseProviderImpl implements SoldierMessageInfoBFFProvider {

    @Autowired
    private SoldierMessageInfoConsumer soldierMessageInfoConsumer;

    @Autowired
    private CommonAdminAreaConsumer commonAdminAreaConsumer;


    @Override
    public BasePagingResponse<List<SoldierMessageInfoDTO>> searchSoldierMessageInfo(final SoldierMessageInfoSearchRequest searchRequest) {

        final BasePagingResponse<List<SoldierMessageInfoDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页SoldierMessageInfo的请求参数不能为空");
            }

            @Override
            public void executeService() {
                BasePagingResponse<List<SoldierMessageInfoDTO>> listBasePagingResponse = soldierMessageInfoConsumer.searchSoldierMessageInfo(searchRequest);
                BeanUtil.copyProperties(listBasePagingResponse, basePagingResponse);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.SOLDIER_MESSAGE_INFO_SEARCH;
            }
        });

        return basePagingResponse;
    }

    @Override
    public BasePagingResponse<List<SoldierMessageInfoDTO>> searchMyMessage(SoldierMessageInfoSearchRequest soldierMessageInfoSearchRequest) {

        soldierMessageInfoSearchRequest.setSoldierId(SessionUtils.getSoldierUserId());
        //soldierMessageInfoSearchRequest.setSoldierId("2021012200143469");
        return soldierMessageInfoConsumer.searchMyMessage(soldierMessageInfoSearchRequest);
    }



    @Override
    public BaseResponse<SoldierMessageInfoDTO> getSoldierMessageInfo(final BaseGetRequest baseGetRequest){

        final BaseResponse<SoldierMessageInfoDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "获取单条SoldierMessageInfo的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)baseGetRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                final BaseResponse<SoldierMessageInfoDTO> soldierMessageInfoResponse = soldierMessageInfoConsumer.getSoldierMessageInfo(baseGetRequest);
                BeanUtil.copyProperties(soldierMessageInfoResponse, baseResponse);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.SOLDIER_MESSAGE_INFO_GET;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<String> insertSoldierMessageInfo(final SoldierMessageInfoInsertRequest insertRequest){

        final BaseResponse<String> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(insertRequest, "新增SoldierMessageInfo的请求参数不能为空");
            }
            @Override
            public void executeService() {
                BaseResponse<String> soldierMessageInfoResponse = soldierMessageInfoConsumer.insertSoldierMessageInfo(insertRequest);
                BeanUtil.copyProperties(soldierMessageInfoResponse, baseResponse);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.SOLDIER_MESSAGE_INFO_INSERT;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<Integer> updateSoldierMessageInfo(final SoldierMessageInfoUpdateRequest updateRequest){

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(updateRequest, "修改SoldierMessageInfo的请求参数不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getId(), "id不能为空");
            }
            @Override
            public void executeService() {
                updateRequest.setCrowd("GOV");
                updateRequest.setBizType("GOV");
                updateRequest.setRead(false);
                updateRequest.setPublished(false);
                updateRequest.setValidPeriod(false);
                updateRequest.setRemind(false);
                SessionUtils.buildModifierOfGov(updateRequest);
                BaseResponse<Integer> soldierMessageInfoResponse = soldierMessageInfoConsumer.updateSoldierMessageInfo(updateRequest);
                BeanUtil.copyProperties(soldierMessageInfoResponse, baseResponse);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.SOLDIER_MESSAGE_INFO_UPDATE;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<Integer> deleteSoldierMessageInfo(final BaseDeleteRequest deleteRequest){

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(deleteRequest, "逻辑删除SoldierMessageInfo的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)deleteRequest.getId(), "id不能为空");
            }
            @Override
            public void executeService() {
                SessionUtils.buildDeleteOfGov(deleteRequest);
                BaseResponse<Integer> soldierMessageInfoResponse = soldierMessageInfoConsumer.deleteSoldierMessageInfo(deleteRequest);
                BeanUtil.copyProperties(soldierMessageInfoResponse, baseResponse);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.SOLDIER_MESSAGE_INFO_DELETE_LOGICALLY;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<List<SoldierMessageInfoDTO>> listRemindAndUnreadMessageInfo() {

        return soldierMessageInfoConsumer.listRemindAndUnreadMessageInfo(SessionUtils.getSoldierUserId());
    }

    @Override
    public BaseResponse<Integer> setMessageRead(SimpleApiRequest request) {

        BizCheckUtil.checkNotNull(request, "请求不能为空");
        BizCheckUtil.checkNotBlank(request.getId(), "id不能为空");

        return soldierMessageInfoConsumer.setMessageRead(request.getId(), SessionUtils.getSoldierUserId());
    }

    @Override
    public BaseResponse<SoldierMessageStatusDTO> countNotReadMessage() {

        return soldierMessageInfoConsumer.countNotReadMessage(SessionUtils.getSoldierUserId());
    }

    @Override
    public BaseResponse<List<SoldierMessageInfoDTO>> listNotSignMessageOfPlacement() {

        return soldierMessageInfoConsumer.listNotSignMessageOfPlacement(SessionUtils.getSoldierUserId());
    }

    @Override
    public BaseResponse<String> addMessageOfPlacement(SoldierMessageInfoInsertRequest soldierMessageInfoInsertRequest) {

        SessionUtils.buildCreatorOfGov(soldierMessageInfoInsertRequest);
        return soldierMessageInfoConsumer.addMessageOfPlacement(soldierMessageInfoInsertRequest);
    }

    @Override
    public BaseResponse<String> addMessageOfGov(SoldierMessageInfoInsertRequest soldierMessageInfoInsertRequest) {

        soldierMessageInfoInsertRequest.setCrowd("GOV");
        soldierMessageInfoInsertRequest.setBizType("GOV");
        soldierMessageInfoInsertRequest.setRead(false);
        soldierMessageInfoInsertRequest.setPublished(false);
        soldierMessageInfoInsertRequest.setValidPeriod(false);
        soldierMessageInfoInsertRequest.setRemind(false);
        SessionUtils.buildCreatorOfGov(soldierMessageInfoInsertRequest);
        return soldierMessageInfoConsumer.insertSoldierMessageInfo(soldierMessageInfoInsertRequest);
    }

    @Override
    public BaseResponse<String> addMessageOfZhyt(SoldierMessageInfoInsertRequest soldierMessageInfoInsertRequest) {

        BizCheckUtil.checkNotNull(soldierMessageInfoInsertRequest, "请求不能为空");
        BizCheckUtil.checkNotBlank(soldierMessageInfoInsertRequest.getTitle(), "标题不能为空");
        BizCheckUtil.checkNotBlank(soldierMessageInfoInsertRequest.getDetail(), "详情不能为空");
        BizCheckUtil.checkIsTrue(!CollectionUtils.isEmpty(soldierMessageInfoInsertRequest.getIdCardNoList()), "身份证列表不能为空");

        if (Strings.isBlank(soldierMessageInfoInsertRequest.getIntroduction())) {
            soldierMessageInfoInsertRequest.setIntroduction(soldierMessageInfoInsertRequest.getTitle());
        }
        soldierMessageInfoInsertRequest.setBizType(ModelIconEnum.QT.name());
        soldierMessageInfoInsertRequest.setMaterialType(MessageMaterialTypeEnum.RTF.name());
        soldierMessageInfoInsertRequest.setRead(false);
        soldierMessageInfoInsertRequest.setPublished(false);
        soldierMessageInfoInsertRequest.setValidPeriod(false);
        soldierMessageInfoInsertRequest.setRemind(false);
        SessionUtils.buildCreatorOfGov(soldierMessageInfoInsertRequest);
        return soldierMessageInfoConsumer.batchAddMessageOfPlacement(soldierMessageInfoInsertRequest);
    }

    @Override
    public BaseResponse<String> addMessageSignOfPlacement(CommonSignInfoInsertRequest request) {

        request.setSoldierId(SessionUtils.getSoldierUserId());
        SessionUtils.buildCreatorOfSoldier(request);
        return soldierMessageInfoConsumer.addMessageSignOfPlacement(request);
    }

    @Override
    public BaseResponse<Integer> publishMessage(SoldierMessageInfoUpdateRequest request) {

        BizCheckUtil.checkNotNull(request, "修改SoldierMessageInfo的请求参数不能为空");
        BizCheckUtil.checkNotBlank(request.getId(), "id不能为空");
        BizCheckUtil.checkNotNull(request.getPublished(), "published不能为空");
        SessionUtils.buildModifierOfGov(request);
        return soldierMessageInfoConsumer.publishMessage(request);
    }

    @Override
    public BasePagingResponse<List<SoldierJobMessageInfoDTO>> searchJobMessageStats(CommonLedgerStatsRequest request) {


        request.setAdminAreaCode(commonAdminAreaConsumer.checkIsChildAdminAreaCode(request.getAdminAreaCode()));
        return soldierMessageInfoConsumer.searchJobMessageStats(request);
    }
}
